unit PesquisaLocacao;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, Buttons, StdCtrls, Grids, DBGrids, DB, Geral,
  ZAbstractRODataset, ZAbstractDataset, ZDataset, JvExControls,
  JvComponent, JvXPCore, JvXPButtons;

type
  TFormPesquisaLocacao = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    Panel4: TPanel;
    Panel5: TPanel;
    dsOrigem: TDataSource;
    DBGridOrigem: TDBGrid;
    EditPesq: TEdit;
    dsDestino: TDataSource;
    QueryOrigem: TZQuery;
    QueryDestino: TZQuery;
    btnIncluirPesq: TJvXPButton;
    JvXPButton1: TJvXPButton;
    JvXPButton2: TJvXPButton;
    JvXPButton3: TJvXPButton;
    JvXPButton4: TJvXPButton;
    JvXPButton5: TJvXPButton;
    JvXPButton6: TJvXPButton;
    QueryOrigemCODLOCACAO: TIntegerField;
    QueryOrigemDATAINSTALACAO: TDateTimeField;
    QueryOrigemDATADESINSTALACAO: TDateTimeField;
    QueryOrigemDATACHEGHORIGEM: TDateTimeField;
    QueryOrigemCODEQUIPAMENTO: TIntegerField;
    QueryOrigemCODCOMBOIO: TIntegerField;
    QueryOrigemCODCLIENTE: TIntegerField;
    QueryOrigemCONTROLETRANSPORTE: TStringField;
    QueryOrigemCODFATURA: TIntegerField;
    QueryOrigemVALORLOCACAO: TFloatField;
    QueryOrigemVALORCOMUNICACAO: TFloatField;
    QueryDestinoCODLOCACAO: TIntegerField;
    QueryDestinoDATAINSTALACAO: TDateTimeField;
    QueryDestinoDATADESINSTALACAO: TDateTimeField;
    QueryDestinoDATACHEGHORIGEM: TDateTimeField;
    QueryDestinoCODEQUIPAMENTO: TIntegerField;
    QueryDestinoCODCOMBOIO: TIntegerField;
    QueryDestinoCODCLIENTE: TIntegerField;
    QueryDestinoCONTROLETRANSPORTE: TStringField;
    QueryDestinoCODFATURA: TIntegerField;
    QueryDestinoVALORLOCACAO: TFloatField;
    QueryDestinoVALORCOMUNICACAO: TFloatField;
    DBGridDestino: TDBGrid;
    procedure FormShow(Sender: TObject);
    function Mostrar (CodCliente, CodFatura : Integer) : String;
    procedure SpeedButton2Click(Sender: TObject);
    procedure SpeedButton3Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Inclui ;
    procedure Exclui ;
    procedure btnIncluirPesqClick(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure JvXPButton1Click(Sender: TObject);
    procedure JvXPButton2Click(Sender: TObject);
    procedure JvXPButton3Click(Sender: TObject);
    procedure JvXPButton4Click(Sender: TObject);
    procedure JvXPButton5Click(Sender: TObject);
    procedure JvXPButton6Click(Sender: TObject);
  private
    { Private declarations }
      OK : Boolean;
      CodFatura : Integer;
  public
    { Public declarations }
  end;

var
  FormPesquisaLocacao: TFormPesquisaLocacao;

implementation

uses DM;


{$R *.dfm}

procedure TFormPesquisaLocacao.FormShow(Sender: TObject);
begin
    SetaDataset(dsOrigem, QueryOrigem);
    SetaDataset(dsDestino, QueryDestino);
    QueryDestino.Connection.StartTransaction;
    QueryDestino.open;
    QueryOrigem.open;
    OK := True;
end;
function TFormPesquisaLocacao.Mostrar (CodCliente, CodFatura : Integer) : String;
begin
    Self.CodFatura := CodFatura;
    with QueryOrigem do
    begin
        close;
        ParamByName('PCODCLIENTE').AsInteger := CodCliente;
    end;
    with QueryDestino do
    begin
        close;
        ParamByName('PCODFATURA').AsInteger := CodFatura;
    end;
    ShowModal;
end;

procedure TFormPesquisaLocacao.SpeedButton2Click(Sender: TObject);
begin
    OK := True;
    Close;
end;

procedure TFormPesquisaLocacao.SpeedButton3Click(Sender: TObject);
begin
    OK := False;
    Close;
end;

procedure TFormPesquisaLocacao.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
    if OK then
        QueryDestino.Connection.Commit
    else
        QueryDestino.Connection.Rollback;
end;

procedure TFormPesquisaLocacao.Inclui;
begin
    with QueryOrigem do
    begin
        edit;
        FieldByname('CODFATURA').AsInteger := CodFatura;
        ApplyUpdates;
    end;
    QueryOrigem.Refresh;
    QueryDestino.Refresh;
end;
procedure TFormPesquisaLocacao.Exclui;
begin
    with QueryDestino do
    begin
        edit;
        FieldByname('CODFATURA').AsVariant := null;
        ApplyUpdates;
    end;
    QueryOrigem.Refresh;
    QueryDestino.Refresh;
end;

procedure TFormPesquisaLocacao.btnIncluirPesqClick(Sender: TObject);
begin
    if dsOrigem.DataSet.Locate('CODLOCACAO', EditPesq.Text, []) then
        Inclui;
    EditPesq.SetFocus;
end;

procedure TFormPesquisaLocacao.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
    SelectNext(ActiveControl as tWinControl, True, True);
    key := 0;
end;

procedure TFormPesquisaLocacao.JvXPButton1Click(Sender: TObject);
begin
    Inclui;
end;

procedure TFormPesquisaLocacao.JvXPButton2Click(Sender: TObject);
begin
    Exclui;
end;

procedure TFormPesquisaLocacao.JvXPButton3Click(Sender: TObject);
var I : Integer;
begin
    with QueryOrigem do
    begin
        First;
        For I := 0 to RecordCount -1 do
        begin
            Inclui;
            Next;
        end;
    end;
end;

procedure TFormPesquisaLocacao.JvXPButton4Click(Sender: TObject);
var I : Integer;
begin
    with QueryDestino do
    begin
        First;
        For I := 0 to RecordCount -1 do
        begin
            Exclui;
            Next;
        end;
    end;
end;

procedure TFormPesquisaLocacao.JvXPButton5Click(Sender: TObject);
begin
    close;
end;

procedure TFormPesquisaLocacao.JvXPButton6Click(Sender: TObject);
begin
    OK := False;
    Close;
end;

end.
